package com.mellarius.activities;

import java.util.Calendar;

import com.mellarius.R;

import com.mellarius.fragments.FragmentRepository;
import com.mellarius.fragments.FragmentRepository.InterventionEdit;
import com.mellarius.fragments.FragmentRepository.InterventionSelectorFragment;
import com.mellarius.fragments.FragmentRepository.MainApiariesFragment;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

public class Intervention extends FragmentActivity {
	final Context context = this;
	private FragmentManager fragManager;
	private FragmentTransaction transaction;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		fragManager = getSupportFragmentManager();
		setContentView(R.layout.activity_interventions);
		transaction = fragManager.beginTransaction();
		transaction.replace(R.id.selectInterventionFrame, new FragmentRepository.InterventionSelectorFragment());
		transaction.commit();
	}
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.action_Apiario:
		Toast.makeText(this, MainApiariesFragment.getApiary().getIdentificacao(), Toast.LENGTH_SHORT).show();
		break;
		default:
		break;
		}
		return super.onOptionsItemSelected(item);
	}
	public void editIntervention(View view) {
			
		if (InterventionSelectorFragment.getCheckedIntervention() == null) {
			InterventionSelectorFragment.getDialog().create().show();
		}else{
			transaction = fragManager.beginTransaction();
			transaction.replace(R.id.interventionOperationsFrame,new FragmentRepository.InterventionEdit());
			InterventionEdit.setType(true);
			transaction.disallowAddToBackStack();
			transaction.commit();
		} 

	}
	public void createIntervention(View view) {

			transaction = fragManager.beginTransaction();
			transaction.replace(R.id.interventionOperationsFrame,new FragmentRepository.InterventionEdit());
			InterventionEdit.setType(false);
			transaction.disallowAddToBackStack();
			transaction.commit();
	}

	public void selectDate(View view) {
		DialogFragment newFragment = new SelectDate();
		newFragment.show(getFragmentManager(), "DatePicker");
	}
	protected void onPause() {

		super.onPause();
		finish();
//		Intent intent = new Intent(this, GestorActividade.class);
//		startActivity(intent);
		
	}
	public void datePut(int year, int month, int day) {
		EditText mEdit;
		mEdit = (EditText)findViewById(R.id.interventionData);
		mEdit.setText(day+"-"+month +"-"+year);	
	}
	@SuppressLint("ValidFragment")
	public class SelectDate extends DialogFragment implements DatePickerDialog.OnDateSetListener {

		public SelectDate() {

		}

		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			Calendar calendar = Calendar.getInstance();
			int yy = calendar.get(Calendar.YEAR);
			int mm = calendar.get(Calendar.MONTH);
			int dd = calendar.get(Calendar.DAY_OF_MONTH) + 1;
			return new DatePickerDialog(getActivity(), this, yy, mm, dd);
		}

		@Override
		public void onDateSet(DatePicker view, int yy, int mm, int dd) {
			datePut(yy, mm + 1, dd);
		}

	}

}
